package com.xh3140.xwidget.dialog.view;

import android.content.Context;
import android.view.Gravity;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.AppCompatTextView;

import com.xh3140.xwidget.dialog.params.TitleParams;

/**
 * 对话框首部视图布局中标题视图
 */
public class TitleView extends AppCompatTextView {

  public TitleView(@NonNull Context context) {
    super(context);
    final HeaderView.LayoutParams layoutParams = new HeaderView.LayoutParams(
            HeaderView.LayoutParams.MATCH_PARENT, HeaderView.LayoutParams.WRAP_CONTENT);
    layoutParams.gravity = Gravity.CENTER_HORIZONTAL;
    setLayoutParams(layoutParams);
  }

  /**
   * 配置视图
   *
   * @param params 标题视图配置属性
   */
  public TitleView configView(@NonNull TitleParams params) {
    setText(params.text);
    setTextSize(params.textSize);
    setTextColor(params.textColor);
    setTypeface(getTypeface(), params.typeface);
    setGravity(params.gravity);
    setBackgroundColor(params.backgroundColor);
    if (params.padding != null) {
      setPaddingRelative(params.padding[0], params.padding[1], params.padding[2], params.padding[3]);
    }
    setVisibility(params.text == null ? GONE : VISIBLE);
    if (params.callback != null) {
      params.callback.callback(this);
    }
    return this;
  }
}
